package com.epidata.exam.data;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

import com.epidata.exam.dao.EmployeeDao;
import com.epidata.exam.model.Employee;

@RequestScoped
public class EmployeeListProducerObserver {

	@Inject
	private EmployeeDao<Employee> empleadoDao;

	private List<Employee> employees;

	// @Named provides access the return value via the EL variable name "members" in the UI (e.g.,
	// Facelets or JSP view)
	@Produces
	@Named
	public List<Employee> getEmployees() {
		return employees;
	}

	public void onEmployeeListChanged(@Observes(notifyObserver = Reception.IF_EXISTS) final Employee employee) {
		retrieveAllMembersOrderedByName();
	}

	@PostConstruct
	public void retrieveAllMembersOrderedByName() {
		employees = empleadoDao.getAllEmployees();
	}
}
